package com.perfectbeauties.admin.parser.sites;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import com.perfectbeauties.admin.parser.IParser;
import com.perfectbeauties.admin.parser.ParserUtil;


public class HegreParser implements IParser {

	// Set to -1 or 0 to turn off sampling
	private static final int SAMPLE = -1;
	
	
	/**
	 * @param args
	 */
	public boolean parse() {
		try {
			FileReader fr = new FileReader(new File("hegre.html"));
			BufferedReader br = new BufferedReader(fr);
			
			int galleryCount = 0;
			
			String line = br.readLine();
			while (line != null) {
				
				if (!line.startsWith("<td><strong>Hegre-Art.com</strong></td>")) {
					line = br.readLine();
					continue;
				}
				
				line = br.readLine();
				while (!line.startsWith("<td>")) {
					line = br.readLine();
				}
				
				line = line.substring(line.indexOf("<td>")+4,line.indexOf("</td>"));
				String name = line.replace("`", "'");
				
				line = br.readLine();
				while (!line.startsWith("<td>")) {
					line = br.readLine();
				}
				
				line = line.substring(line.indexOf("<td>")+4,line.indexOf("</td>"));
				String desc = line.replace("`", "'");
				
				line = br.readLine();
				while (!line.startsWith("<td>")) {
					line = br.readLine();
				}
				
				line = line.substring(line.indexOf("'")+1,line.indexOf("'>"));
				String zip = line;
				
				String result = ParserUtil.submit(name, desc, 4, zip);
				
				if (result.equals("Warning:Exists")) {
	            	break;
	            }
				
				System.out.println(result+" ("+name+")");
								
				line = br.readLine();
				
				galleryCount++;
				
				if (SAMPLE > 0 && galleryCount >= SAMPLE) {
					break;
				}
			}
			
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	

	}
	
	

}
